#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.h>
#if defined(TEST_TARGET_crc)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/crc.h>
#else  
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "crc.h"
#endif

/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/

/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/

uint32_t std_crc(uint8_t* data, uint32_t len, uint32_t index)
{
    uint32_t crc = 0;

    switch (index)
    {
    case 0: crc = crc4_itu(data, len); break;
    case 1: crc = crc5_epc(data, len); break;
    case 2: crc = crc5_itu(data, len); break;
    case 3: crc = crc5_usb(data, len); break;
    case 4: crc = crc6_itu(data, len); break;
    case 5: crc = crc7_mmc(data, len); break;
    case 6: crc = crc8(data, len); break;
    case 7: crc = crc8_itu(data, len); break;
    case 8: crc = crc8_rohc(data, len); break;
    case 9: crc = crc8_maxim(data, len); break;
    case 10: crc = crc16_ibm(data, len); break;
    case 11: crc = crc16_maxim(data, len); break;
    case 12: crc = crc16_usb(data, len); break;
    case 13: crc = crc16_modbus(data, len); break;
    case 14: crc = crc16_ccitt(data, len); break;
    case 15: crc = crc16_ccitt_false(data, len); break;
    case 16: crc = crc16_x25(data, len); break;
    case 17: crc = crc16_xmodem(data, len); break;
    case 18: crc = crc16_dnp(data, len); break;
    case 19: crc = crc32(data, len); break;
    case 20: crc = crc32_mpeg_2(data, len); break;
    default:
        break;
    }

    return crc;
}

static void test_base(void)
{
    char *testSample[] = {
        "Hello",
        "crc algorithms",
        "123456789",
    };
    uint32_t crc0 = 0, crc1 = 0;

    for (int i = 0; i < sizeof(testSample) / sizeof(testSample[0]); i++)
    {
        printf("testSample %d\r\n", i);
        for (int j = 0; j < sizeof(crcParaModelTable) / sizeof(crcParaModelTable[0]); j++)
        {
            crc0 = std_crc(testSample[i], strlen(testSample[i]), j);
            crc1 = crc(testSample[i], strlen(testSample[i]), &crcParaModelTable[j]);
            printf("crc %02d, crc0 %08X, crc1 %08X, same %d\r\n", j, crc0, crc1, (crc0 == crc1) ? 1 : 0);
        }
    }
}

/************************************************************************************/
/*************************************  Command  ************************************/
/************************************************************************************/

static void usage(void)
{
    printf(
"Usage: crc [opt] [arg] ...\n"
"\n"
"options:\n"
"    -e <execute>        Specifies the function to execute, the default is the <base> test\n"
"                        <base>      Test base function\n"
"                        <cal>       Calculate the check value\n"
"    -h                  Print help\n"
"    -v                  Print version\n"
"    -c [<check>]        CRC algorithm, default `CRC-4/ITU`\n"
"                        0  : CRC-4/ITU          \n"
"                        1  : CRC-5/EPC          \n"
"                        2  : CRC-5/ITU          \n"
"                        3  : CRC-5/USB          \n"
"                        4  : CRC-6/ITU          \n"
"                        5  : CRC-7/MMC          \n"
"                        6  : CRC-8              \n"
"                        7  : CRC-8/ITU          \n"
"                        8  : CRC-8/ROHC         \n"
"                        9  : CRC-8/MAXIM        \n"
"                        10 : CRC-16/IBM         \n"
"                        11 : CRC-16/MAXIM       \n"
"                        12 : CRC-16/USB         \n"
"                        13 : CRC-16/MODBUS      \n"
"                        14 : CRC-16/CCITT       \n"
"                        15 : CRC-16/CCITT-FALSE \n"
"                        16 : CRC-16/X25         \n"
"                        17 : CRC-16/XMODEM      \n"
"                        18 : CRC-16/DNP         \n"
"                        19 : CRC-32             \n"
"                        20 : CRC-32/MPEG-2      \n"
"    -s <string>         Check string\n"
"\n"
    );
}

static int test(int argc, char *argv[])
{
    char *execute = NULL;
    int index = 0;
    char *s = NULL;

    /* reset getopt */
    command_opt_init();

    while (1)
    {
        int opt = command_getopt(argc, argv, "e:hvu::c:s:");
        if (opt == -1) break;

        switch (opt) 
        {
        case 'c' :
            if (command_optarg)
            {
                index = atoi(command_optarg);
                if (index >= (sizeof(crcParaModelTable) / sizeof(crcParaModelTable[0])))
                {
                    printf("No such crc functiion!\r\n");
                    return -1;
                }
            }
            break;
        case 's' :
            s = command_optarg;
            break;
        case 'e' :
            execute = command_optarg;
            break;
        case 'v' :
            printf("crc version %d.%d.%d\r\n", CRC_V_MAJOR, CRC_V_MINOR, CRC_V_PATCH);
            return 0;
        case '?':
            printf("Unknown option `%c`\r\n", command_optopt);
            return -1;
        case 'h' : 
        default:
            usage();
            return 0;
        }
    }

    if (execute)
    {
        if (!strcmp(execute, "base"))
        {
            test_base();
        }
        else if (!strcmp(execute, "cal"))
        {
            printf("check value: 0x%X, 0x%X\r\n", std_crc(s, strlen(s), index), crc(s, strlen(s), &crcParaModelTable[index]));
        }
    }
    else  
    {
        test_base();
    }
    
    return 0;
}

/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/

#if defined(TEST_TARGET_crc)
int main(int argc, char *argv[])
{
    return test(argc, argv);
}
#else 
void test_crc(void)
{
    command_export("crc", test);
}
init_export_app(test_crc);
#endif 
